package com.east.service.fee.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.east.bean.DictionaryItems;
import com.east.bean.Fee;
import com.east.bean.Income;
import com.east.bean.OtherFee;
import com.east.bean.Project;
import com.east.bean.ReimburseRecords;
import com.east.bean.Staff;
import com.east.controller.fee.info.Subject;
import com.east.dao.DictionaryItemsMapper;
import com.east.dao.IncomeMapper;
import com.east.dao.ProjectMapper;
import com.east.dao.ReimburseRecordsMapper;
import com.east.dao.StaffMapper;
import com.east.exception.DescribeException;
import com.east.service.fee.ReimburseService;
import com.east.utils.DateUtils;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
@Service("reimburseService")
public class ReimburseServiceImpl implements ReimburseService {
	@Autowired
	ReimburseRecordsMapper reimburseRecordsMapper;
	@Autowired 
	StaffMapper staffMapper;
	@Autowired 
	ProjectMapper projectMapper;
	@Autowired 
	DictionaryItemsMapper dictionaryItemsMapper;
	@Override
	public Page<ReimburseRecords> findByPage(Pagenation pagenation, String key,String subject,String startTime ,String endTime) {
		Page<ReimburseRecords> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		if(StringUtils.isBlank(endTime)) {
			endTime= DateUtils.getDaysOfDate(new Date());
		}
		reimburseRecordsMapper.finByReimburseAll(key,subject,startTime,endTime);
		return startPage;
	}

	@Override
	public boolean saveReimburseRecords(ReimburseRecords reimburseRecords) {
		ReimburseRecords re = new ReimburseRecords();
		re.setRrAcount(reimburseRecords.getRrAcount());//金额
		if(StringUtils.isNotBlank(reimburseRecords.getDateTime())) {
			re.setRrHandTime(DateUtils.getDate(reimburseRecords.getDateTime()));//时间
		}
		re.setRrMan(reimburseRecords.getRrMan());//经办人
		re.setRrPaperSerial(com.east.utils.StringUtils.random());//报销单编号
		re.setRrProject(reimburseRecords.getRrProject());//项目名称
		re.setRrSubject(reimburseRecords.getRrSubject());//所属项目id
		re.setRrSummary(reimburseRecords.getRrSummary());
		re.setRrType(reimburseRecords.getRrType());//付款类型
		re.setRrUuid(UUID.randomUUID().toString());
		re.setStaffId(reimburseRecords.getStaffId());//经办人id
		int insert = reimburseRecordsMapper.insert(re);
		if(insert>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updateReimburseRecordse(ReimburseRecords reimburseRecords) {
		ReimburseRecords re = reimburseRecordsMapper.selectByPrimaryKey(reimburseRecords.getRrId());
		re.setRrAcount(reimburseRecords.getRrAcount());//金额
		if(StringUtils.isNotBlank(reimburseRecords.getDateTime())) {
			re.setRrHandTime(DateUtils.getDate(reimburseRecords.getDateTime()));//时间
		}
		re.setRrProject(reimburseRecords.getRrProject());//项目名称
		re.setRrSubject(reimburseRecords.getRrSubject());//所属项目id
		re.setRrSummary(reimburseRecords.getRrSummary());
		re.setRrType(reimburseRecords.getRrType());//付款类型
		re.setStaffId(reimburseRecords.getStaffId());//经办人id
		int insert = reimburseRecordsMapper.updateByPrimaryKey(re);
		if(insert>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean delReimburseRecords(ReimburseRecords reimburseRecords) {
		int deleteByPrimaryKey = reimburseRecordsMapper.deleteByPrimaryKey(reimburseRecords.getRrId());
		if(deleteByPrimaryKey>0) {
			return true;
		}
		return false;
	}

	@Override
	public ReimburseRecords findByOne(ReimburseRecords reimburseRecords) {
		// TODO Auto-generated method stub
		return reimburseRecordsMapper.selectByPrimaryKey(reimburseRecords.getRrId());
	}

	@Override
	public String saveListReimburse(List<ReimburseRecords> list) {
		for (ReimburseRecords fe : list) {
			fe.setRrUuid(UUID.randomUUID().toString());
			fe.setRrPaperSerial(com.east.utils.StringUtils.random());//报销单编号
			try {
				Staff findByName = staffMapper.findByName(fe.getStaffName());//经办人
				fe.setStaffId(findByName.getsId());
			} catch (DescribeException e) {
				new DescribeException("经办人重名，请联系人事部门！！");
				return "经办人重名，请联系人事部门！！";
			}
			try {
				DictionaryItems findByName = dictionaryItemsMapper.findByName( fe.getSubjectName());//科目名称
				fe.setRrSubject(findByName.getDiId());
			} catch (DescribeException e) {
				new DescribeException("科目重复,请查看基本设置科目管理");
				return "科目重复,请查看基本设置科目管理";
			}
			try {
				DictionaryItems findByName = dictionaryItemsMapper.findByName(fe.getType());//付款类型
				fe.setRrType(findByName.getDiId());
			} catch (DescribeException e) {
				new DescribeException("付款类型重复,请查看基本设置付款类型");
				return "付款类型重复,请查看基本设置付款类型";
			}
			
		}
		int insertList = reimburseRecordsMapper.insertList(list);
		if(insertList>0) {
			return "添加成功";
		}else {
			return "添加失败";
		}
		
	}
	@Override
	public List<Subject> fingBySubject(String year, String month) {
		return reimburseRecordsMapper.findBySubject(year, month);
	}
}
